﻿using System;
using System.Data;

namespace DataScripter.Database
{
	public static class DbReaderExtensions
	{
		public static string GetString(this IDataReader reader, string columnName)
		{
			int ordinal = reader.GetOrdinal(columnName);
			if (reader.IsDBNull(ordinal))
				return null;
			return reader.GetString(ordinal);
		}

		public static bool GetBoolean(this IDataReader reader, string columnName)
		{
			return reader.GetBoolean(reader.GetOrdinal(columnName));
		}
		public static Guid GetGuid(this IDataReader reader, string columnName)
		{
			return reader.GetGuid(reader.GetOrdinal(columnName));
		}
		public static decimal GetDecimal(this IDataReader reader, string columnName)
		{
			return reader.GetDecimal(reader.GetOrdinal(columnName));
		}
		public static decimal? GetNullableDecimal(this IDataReader reader, string columnName)
		{
			int ordinal = reader.GetOrdinal(columnName);
			if (reader.IsDBNull(ordinal))
				return null;
			return reader.GetDecimal(ordinal);
		}

		public static int GetInt32(this IDataReader reader, string columnName)
		{
			return reader.GetInt32(reader.GetOrdinal(columnName));
		}
        public static int? GetNullableInt32(this IDataReader reader, string columnName)
        {
            int ordinal = reader.GetOrdinal(columnName);
            if (reader.IsDBNull(ordinal))
                return null;
            return reader.GetInt32(ordinal);
        }
        public static int? GetNullableInt16(this IDataReader reader, string columnName)
        {
            int ordinal = reader.GetOrdinal(columnName);
            if (reader.IsDBNull(ordinal))
                return null;
            return reader.GetInt16(ordinal);
        }
        public static bool? GetNullableBoolean(this IDataReader reader, string columnName)
        {
            int ordinal = reader.GetOrdinal(columnName);
            if (reader.IsDBNull(ordinal))
                return null;
            return reader.GetBoolean(ordinal);
        }
        public static byte? GetNullableByte(this IDataReader reader, string columnName)
        {
            int ordinal = reader.GetOrdinal(columnName);
            if (reader.IsDBNull(ordinal))
                return null;
            return reader.GetByte(ordinal);
        }
        public static double? GetNullableFloat(this IDataReader reader, string columnName)
        {
            int ordinal = reader.GetOrdinal(columnName);
            if (reader.IsDBNull(ordinal))
                return null;
            return reader.GetDouble(ordinal);
        }
        public static DateTime GetDateTime(this IDataReader reader, string columnName)
		{
			return reader.GetDateTime(reader.GetOrdinal(columnName));
		}
		public static DateTime? GetNullableDateTime(this IDataReader reader, string columnName)
		{
			int ordinal = reader.GetOrdinal(columnName);
			if (reader.IsDBNull(ordinal))
				return null;
			return reader.GetDateTime(ordinal);
		}
        public static Guid? GetNullableGuid(this IDataReader reader, string columnName)
        {
            int ordinal = reader.GetOrdinal(columnName);
            if (reader.IsDBNull(ordinal))
                return null;
            return reader.GetGuid(ordinal);
        }

	}
}
